package com.lh.blogConsumer.controller.api;

import com.lh.blogCommon.result.LhR;
import com.lh.blogCommon.result.ResultCode;
import com.lh.blogCommon.to.PhotoTo;
import com.lh.blogCommon.util.PageUtils;
import com.lh.blogConsumer.controller.admin.FeaturedIrstController;
import com.lh.blogConsumer.controller.admin.PersonalCenterController;
import com.lh.blogConsumer.entity.FeaturedIrstEntity;
import com.lh.blogConsumer.entity.PersonalCenterEntity;
import com.lh.blogConsumer.entity.PersonalSkillsEntity;
import com.lh.blogConsumer.feign.TriviaFeignService;
import com.lh.blogConsumer.service.FeaturedIrstService;
import com.lh.blogConsumer.service.PersonalCenterService;
import com.lh.blogConsumer.service.PersonalSkillsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//创建时间 2021/3/23
//创建人 lh
//描述
@Controller("/view")
@Slf4j
public class viewController {

    @Autowired
    PersonalCenterController personalCenterController;
    @Autowired
    FeaturedIrstService featuredIrstService;
    @Autowired
    TriviaFeignService triviaFeignService;
    @Autowired
    PersonalSkillsService personalSkillsService;

    @GetMapping("/welcome")
    public String welcome(){
        return "welcome";
    }

    @GetMapping({"/","/index"})
    public String index(Model model){
        Map<String, Object> params = new HashMap<>();
        params.put("key","1041416140");
        LhR list = personalCenterController.list(params);
        if(list.getCode().equals(ResultCode.SUCCESS.getCode())){
            PageUtils page = (PageUtils) list.getData().get("page");
            PersonalCenterEntity attributeValue = (PersonalCenterEntity) page.getList().get(0);
            model.addAttribute("data", attributeValue);
        }else {
            log.error(list.getCode()+list.getMessage());
        }
        return "index";
    }

    @GetMapping("/main")
    public String main(Model model){
        //轮播图信息
        List<FeaturedIrstEntity> featuredIrstEntityList = featuredIrstService.list();
        model.addAttribute("featuredList",featuredIrstEntityList);
        //照片墙
//        HashMap<String, Object> triviaMap = new HashMap<>();
        Map<String, Object> triviaMap = new HashMap<>();
        triviaMap.put("key","");
        LhR list = triviaFeignService.list(triviaMap);
        if(list.getCode().equals(ResultCode.SUCCESS.getCode())){

            LinkedHashMap page = (LinkedHashMap) list.getData().get("page");
            List<PhotoTo> photoTos = (List<PhotoTo>) page.get("list");
//            PageUtils page = (PageUtils) list.getData().get("page");
//            List<PhotoTo> photoTos = (List<PhotoTo>) page.getList();
            model.addAttribute("photoTos", photoTos);
        }else {
            log.error(list.getCode()+list.getMessage());
        }
        //个人技能
        List<PersonalSkillsEntity> personalSkillsList = personalSkillsService.list();
        model.addAttribute("personalSkillsList",personalSkillsList);

        return "main";
    }

}
